﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;

public partial class Users_UpdateFindFamily : BasePage
{
    FindFamilyBLL bll = new FindFamilyBLL();
    protected int TempleId = 0;
    protected AspNetCs.Bll.Temple templateBLL = AspNetCs.Bll.TempleBll.CreateNew;

    protected string notSet = "display:none;";
    protected string play = "display:block;";
    protected string musicPath = "";

    protected int MusicId = 0;

    protected int id = 0;

    protected DataTable dataTable = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        //CheckUserExit("Login.aspx","请您登录!",true);
        CheckUserExit("Login.Aspx", "请您登录", true);
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                ViewState["id"] = Request["id"];
            }
            BindKindsOfDataControl();
            InitPage();
        }
        else
        {
            id = StringHelper.isNumber_int(ViewState["id"].ToString());
            dataTable = bll.SearchFamilyInfoById("*", id);
            ViewState["FindFamily"] = dataTable;
        }

        MusicId = StringHelper.isNumber_int(dataTable.Rows[0]["MusicId"].ToString());
        initMusic();
        //t = ddlMuType.SelectedValue;
        //ArrayList list = new ArrayList();
        //DataTable dt = salBll.GetNetMuIdDataTable(" and SaleType=" + (int)Channel_Type.memorial + "  and  UserId=0");
        //foreach (DataRow dr in dt.Rows)
        //{
        //    list.Add(dr["Number"]);
        //}
        //DefaultValue = bll.RndNum(list, RndLen);
        //this.txtNetMuId.Text = DefaultValue;
    }

    private void InitPage()
    {


        if (ViewState["id"] != null)
        {
            id = StringHelper.isNumber_int(ViewState["id"].ToString());
            dataTable = bll.SearchFamilyInfoById("*", id);
            ViewState["FindFamily"] = dataTable;
        }
        if (dataTable != null)
        {
            if (dataTable.Rows.Count > 0)
            {
                txtPreName.Text = dataTable.Rows[0]["PreName"].ToString();
                txtPostfixName.Text = dataTable.Rows[0]["PostfixName"].ToString();
                Txt_otherName.Text = dataTable.Rows[0]["OtherName"].ToString();
                Radio_Sex.SelectedValue = dataTable.Rows[0]["Sex"].ToString();
                txtAge.Text = dataTable.Rows[0]["Age"].ToString();

                DD_TimeType.SelectedValue = dataTable.Rows[0]["BirthType"].ToString();

                if (DD_TimeType.SelectedValue == TimeType_enum.pub.ToString())
                {
                    Txt_birthyear.Text = dataTable.Rows[0]["Birthday"].ToString();
                }
                else
                {
                    Txt_birthyear.Text = dataTable.Rows[0]["Birthday"].ToString().ToString().Trim();
                    //Txt_birthyear.Text = dataTable.Rows[0]["DepartBirthday"].ToString();
                }
                txtLostPlace.Text = dataTable.Rows[0]["LostPlace"].ToString();
                txtCountry.Text = dataTable.Rows[0]["Country"].ToString();
                Txt_Race.Text = dataTable.Rows[0]["Nation"].ToString();
                Txt_place.Text = dataTable.Rows[0]["BirthPlace"].ToString();
                Txt_Occu.Text = dataTable.Rows[0]["Work"].ToString();
                txtPhone.Text = dataTable.Rows[0]["Phone"].ToString();
                txtTelephone.Text = dataTable.Rows[0]["Telephone"].ToString();
                txtEmail.Text = dataTable.Rows[0]["Email"].ToString();
                txtQQ.Text = dataTable.Rows[0]["QQ"].ToString();
                txtContactAddress.Text = dataTable.Rows[0]["ContactAddress"].ToString();
                Txt_UpImg.Text = dataTable.Rows[0]["Photo"].ToString();
                WebEditor1.Text = dataTable.Rows[0]["LostPerInfo"].ToString();
                Find_Title.Text = dataTable.Rows[0]["Title"].ToString();
                MusicBLL musicBLL = new MusicBLL();
                musicBLL.BindSelectedMusic(StringHelper.isNumber_int(dataTable.Rows[0]["MusicId"].ToString()), ddlMusic);

                this.TempleId = StringHelper.isNumber_int(dataTable.Rows[0]["TempleId"].ToString());
            }
        }
    }

    public void initMusic()
    {
        if (MusicId == 0)
        {
            notSet = "display:block;";
            play = "display:none;";
        }
        else
        {
            MusicBLL musicBll = Music_Bll.CreateNew;
            MusicModel musicModel = new MusicModel();
            DataTable dataTable = musicBll.GetDataTable("id=" + MusicId);
            if (dataTable.Rows.Count > 0)
            {
                musicModel = musicBll.GetModel(dataTable.Rows[0]);
            }
            if (musicModel.MusicType == 0)
            {
                musicPath = "../" + musicModel.MusicPath;
            }
            else
            {
                musicPath = musicModel.MusicPath;
            }
        }
    }

    private void BindKindsOfDataControl()
    {
        EnumDescription[] Sex_Ed = EnumDescription.GetFieldTexts(typeof(Sex_Enum), EnumDescription.SortType.Rank);
        EnumDescription[] Ed = EnumDescription.GetFieldTexts(typeof(TimeType_enum), EnumDescription.SortType.Rank);

        this.Radio_Sex.DataTextField = "EnumDisplayText";
        this.Radio_Sex.DataValueField = "EnumDisplayText";
        this.Radio_Sex.DataSource = Sex_Ed;
        this.Radio_Sex.DataBind();
        this.Radio_Sex.SelectedIndex = 0;
        this.Radio_Sex.RepeatColumns = 5;

        this.DD_TimeType.DataTextField = "EnumDisplayText";
        this.DD_TimeType.DataValueField = "EnumDisplayText";
        this.DD_TimeType.DataSource = Ed;
        this.DD_TimeType.DataBind();
        this.DD_TimeType.SelectedIndex = 0;

    }


    public FindFamilyModel getModel()
    {
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateNetMu.Aspx", "请您登录", true);
        FindFamilyModel model = new FindFamilyModel();
        DateTime RegisterTime = DateTime.Now;
        Users_Model user = Base_GetUserInfo;
        int CreateId = 0;
        if (user.UserId < 1)
        {
            ResultStr("Login.aspx", "请您登录!");
        }
        CreateId = user.UserId;
        int IsCheck = 0;
        int IsDel = 0;
        int AcceeCount = 0;
        int TempleId = 0;
        int MusicId = StringHelper.isNumber_int(ddlMusic.SelectedValue);
        DataTable table = null;
        if (ViewState["FindFamily"] != null)
        {
            table = ViewState["FindFamily"] as DataTable;

        }
        if (table != null)
        {
            model.Id = StringHelper.isNumber_int(table.Rows[0]["Id"].ToString());
            IsCheck = StringHelper.isNumber_int(table.Rows[0]["IsCheck"].ToString());
            IsDel = StringHelper.isNumber_int(table.Rows[0]["IsDel"].ToString());
            AcceeCount = StringHelper.isNumber_int(table.Rows[0]["AcceeCount"].ToString());
            RegisterTime = DateTime.Parse(table.Rows[0]["RegisterTime"].ToString());
        }

        model.ItemName = 0;
        model.PreName = txtPreName.Text;
        model.PostfixName = txtPostfixName.Text;
        model.OtherName = Txt_otherName.Text;
        model.Sex = Radio_Sex.SelectedValue;
        model.Age = StringHelper.isNumber_int(txtAge.Text);
        model.BirthType = DD_TimeType.SelectedValue;
        model.Birthday = Txt_birthyear.Text;
        if (model.BirthType == TimeType_enum.pub.ToString())
        {
            model.Birthday = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
            if (StringHelper.isDatetime_Bool(model.Birthday))
            {
                model.DepartBirthday = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.Birthday));
            }
        }
        else
        {
            model.DepartBirthday = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
        }
        model.LostPlace = txtLostPlace.Text;
        model.Nation = Txt_Race.Text;
        model.Country = txtCountry.Text;
        model.BirthPlace = Txt_place.Text;
        model.Work = Txt_Occu.Text;
        model.Phone = txtPhone.Text;
        model.Telephone = txtTelephone.Text;
        model.Email = txtEmail.Text;
        model.Qq = txtQQ.Text;
        model.ContactAddress = txtContactAddress.Text;
        model.LostPerInfo = WebEditor1.Text;
        model.RegisterTime = RegisterTime;
        model.CreateId = CreateId;
        model.IsCheck = IsCheck;
        model.IsDel = IsDel;
        model.Photo = Txt_UpImg.Text;
        model.AcceeCount = AcceeCount;
        model.TempleId = TempleId;
        model.MusicId = MusicId;
        model.Title = StringHelper.HtmlEncrypt(StringHelper.RemoveHtml(Find_Title.Text));
        return model;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (StringHelper.HtmlEncrypt(StringHelper.RemoveHtml(Find_Title.Text)) == "")
        {
            Response.Write("<script>alert('标题不能为空！');location.href='CreateFindFamily.aspx';</script>");
            Response.End();
        }

        FindFamilyModel model = getModel();
        bll.UpdateFamilyInfo(model);
        DrePoint();
        ResultStr("FindFamilyList.aspx", "修改成功!");
    }

    protected void DrePoint()
    {
        int ClanTempleId = id;
        //AncestralHall_Model ClanMd = AncestralHall_Bll.CreatNew.GetModel(ClanTempleId);

        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        //int needPoint = CreateNetMuPoint(NetMuTypeEnum(model.MuType));
        decimal TemplatePoint = StringHelper.isNumber_decimal(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);
        //if (TempId == this.TempleId)
        //{
        //    return;
        //}
        //if (UserId != 0)
        //{
        //    if (TemplatePoint != 0)
        //    {
        //        userBll.DrePoint(TemplatePoint, UserId);
        //    }
        //}
        //else
        //{
        //    CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/UpdateFindFamily_" + ViewState["Id"] + ".aspx", "请您登录", true);
        //}

        if (TempId == this.TempleId)
        {
            Response.Write("<script>alert('保存成功!');location.href='UpdateFindFamily_" + id + ".Aspx';</script>");
            return;
        }
        if (UserId != 0)
        {
            if (TemplatePoint != 0)
            {
                if (TemplatePoint <= Base_GetUserInfo.Point)
                {

                    Temple_Model TmpMd = templateBLL.GetModel(TempId);
                    if (TmpMd.Id > 0)
                    {

                        Point_Log_Model PointMd = new Point_Log_Model();
                        PointMd.Addtime = DateTime.Now;
                        PointMd.NetIp = StringHelper.Ip();
                        PointMd.Point = TemplatePoint;
                        PointMd.Readme = "用户修改" + EnumDescription.GetFieldText(Channel_Type.FindFamily) + " 模板为" + TmpMd.TempleName + "，扣除点数" + TemplatePoint + "点";
                        PointMd.UserId = UserId;
                        PointMd.UserType = (int)UserTypeEnum.User;
                        Point_Log_Bll.CreateNew.Add(PointMd);
                    }
                    else
                    {
                        Response.Write("<script>alert('保存失败,当前模板不存在!');location.href='UpdateFindFamily_" + id + ".Aspx';</script>");
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('保存失败,用户点数不足!');location.href='UpdateFindFamily_" + id + ".Aspx';</script>");
                    return;
                }


            }
            FindFamily_Bll.CreatNew.filterUpdate(" TempleId= " + TempId, " id=" + id);
            //NetMu_Bll.CreatNew.filterUpdate(" TemplateId= " + TempId, " id=" + ViewState["Id"]);
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/UpdateFindFamily_" + id + ".Aspx", "请您登录", true);
        }
        Response.Write("<script>alert('保存成功!');location.href='UpdateFindFamily_" + id + ".Aspx';</script>");


    }

    protected void btnMusic_Click(object sender, EventArgs e)
    {
        int MusicId = StringHelper.isNumber_int(ddlMusic.SelectedValue);
        //if (MusicId > 0)
        //{
        bll.SetMusic(id, MusicId);
        //}
        Response.Redirect("UpdateFindFamily_" + id + ".Aspx");
    }
}
